package org.fzu.ercha.wuta.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import org.fzu.ercha.wuta.Adapter.SongAdapter;
import org.fzu.ercha.wuta.Entity.Audio;
import org.fzu.ercha.wuta.MyApplication;
import org.fzu.ercha.wuta.Util.SharedPreferencesUtil;
import org.fzu.ercha.wuta.R;
import org.fzu.ercha.wuta.Util.LogUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by ercha on 2017/1/26.
 */

public class SongFragment extends Fragment {
    private Context mContext = null;
    private View mView = null;
    private String className = getClass().getName();

    private ListView mListView = null;
    private SongAdapter mAdapter = null;

    private OnSongListChangedListener mSongListChangedListener = null;
    private OnClickListListener mClickListListener = null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        mContext = getActivity();

        View view = inflater.inflate(R.layout.fragment_song, container, false);
        this.mView = view;

        LogUtil.i(className, "here onCreateView");
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.i(className, "onStart");

        mListView = (ListView)mView.findViewById(R.id.songList);

        mAdapter = new SongAdapter(mContext, R.layout.item_song, MyApplication.getAudioList());
        mAdapter.setOnSongListChangedListener(new SongAdapter.OnSongListChangedListener() {
            @Override
            public void onSongDeleted() {
                if(mSongListChangedListener != null){
                    mSongListChangedListener.onSongDeleted();
                }
            }
        });
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtil.i("SongList click", position+"");
                if(mClickListListener != null){
                    mClickListListener.onClick(MyApplication.getAudioByIndex(position));
                }
            }
        });
    }

    public interface OnSongListChangedListener{
        void onSongDeleted();
    }
    public void setOnSongListChangedListener(OnSongListChangedListener listener){
        mSongListChangedListener = listener;
    }

    public interface OnClickListListener{
        void onClick(Audio audio);
    }
    public void setOnClickListListener(OnClickListListener listListener){
        mClickListListener = listListener;
    }
}
